package com.qss.service.impl;

import com.qs.mapper.SysResourceMapper;
import com.qs.pojo.SysResource;
import com.qs.pojo.SysResourceExample;
import com.qss.service.ResourceService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Description
 * @user HP
 * @email 1460961774@qq.com
 * @Date 2021-10-01 18:53
 * @packageName com.qss.service.impl
 */
@Service(interfaceClass = ResourceService.class,timeout = 10000)
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private SysResourceMapper mapper;
    @Override
    public List<SysResource> queryAllResources(Integer roleId) {
        return mapper.selectRoleResource(roleId);
    }

    @Override
    public SysResource queryResourceById(Integer resourceId) {
        return mapper.selectByPrimaryKey(resourceId);
    }

    @Override
    public boolean saveResource(SysResource resource) {
        if (resource.getResourceId()!=null){
            return mapper.updateByPrimaryKeySelective(resource)==1;
        }
        return mapper.insertSelective(resource)==1;
    }

    @Override
    public boolean delUserById(Integer[] ids) {
       int num =0;
        for (Integer id : ids) {
           num += mapper.deleteByPrimaryKey(id);
        }
        return num==ids.length;
    }

    @Override
    public SysResource queryResourceByName(String resourceName) {
        SysResourceExample sysResourceExample = new SysResourceExample();
        SysResourceExample.Criteria criteria = sysResourceExample.createCriteria();
        criteria.andResourceNameEqualTo(resourceName);
        List<SysResource> sysResources = mapper.selectByExample(sysResourceExample);
        if (sysResources==null||sysResources.size()==0){
            return null;
        }
        return sysResources.get(0);
    }

    @Override
    public List<SysResource> queryResources(SysResource resource) {
        SysResourceExample example = new SysResourceExample();
        SysResourceExample.Criteria criteria = example.createCriteria();
        if (resource!=null){
            if (!StringUtils.isEmpty(resource.getResourceName())){
                criteria.andResourceNameLike("%"+resource.getResourceName()+"%");
            }
            if (!StringUtils.isEmpty(resource.getParentId())){
                criteria.andParentIdEqualTo(resource.getParentId());
            }

        }
        return mapper.selectByExample(example);

    }
}
